<html><head></head>
<body onload="doOnLoad()">

<?php
 
 require_once "cw2conf.php";
 include_once "misc.php";

 $dbl=DBConn();
 $tournament_id = 0;
 if (IsSet($_POST['tournament_id']))
 {
  $tournament_id = IntVal($_POST['tournament_id']);
 }
 else if (IsSet($_GET['tournament_id']))
 {
  $tournament_id = IntVal($_GET['tournament_id']);
 }

$logged_player_id = CW2_NO_OBJECT;
$logged_tplayer_id = CW2_NO_OBJECT;
$logged_tgroup_id = CW2_NO_OBJECT;
$logged_permissions = 0;
if (IsSet($_POST['player_id']))
{
 $logged_player_id = IntVal($_POST['player_id']);
}
else if (IsSet($_GET['player_id']))
{
 $logged_player_id = IntVal($_GET['player_id']);
}

 do
 {
  if (($tournament_id == 0) || ($logged_player_id < 0))
  {
   echo 'POST = ';
   var_dump($_POST);
   echo '<br>GET = ';
   var_dump($_GET);
   break;
  }
 
 global $tournament, $tgroups, $tplayers; 
 
 if (GetTournament($tournament_id)<0) {return;}
 if (GetTournamentGroups($tournament_id)<0) {return;}
 if (GetTournamentPlayers($tournament_id)<0) {return;}
 
 if (IsSet($_POST['changed_tplayer_id']))
 {
  $changed_tplayer_id = $_POST['changed_tplayer_id'];
  
  if (IsSet($tplayers[$changed_tplayer_id]))
  {
   if (($logged_permissions & CW2_TOURNAMENT_MODERATOR) || (($logged_permissions & CW2_CLAN_ADMIN) && ($tplayers[$changed_tplayer_id]['tgroup_id'] == $logged_tgroup_id)))
   {
    $cw2_player_flags = $tplayers[$changed_tplayer_id]['cw2_player_flags'];
    //echo "<br>Start flags = $cw2_player_flags";
    $cw2_player_flags = $cw2_player_flags ^ ($cw2_player_flags & CW2_MAX_CLAN_PERMISSIONS);
    //echo "<br>Xor flags = $cw2_player_flags";
    $curr_permission = 1;
    while ($curr_permission <= CW2_MAX_CLAN_PERMISSIONS)
    {
     $permission_param = "permission_" . $curr_permission;
     //echo "<br>checking permission '$permission_param'";
     if (IsSet($_POST[$permission_param]))
     {
      $cw2_player_flags += $curr_permission;
      //echo "<br>+permission = $curr_permission >> $cw2_player_flags";
     }
     $curr_permission *= 2;
    }
    //echo "<br>End flags = $cw2_player_flags";
    $sql = "UPDATE tm_players SET cw2_player_flags = ".$cw2_player_flags." WHERE id = ".$changed_tplayer_id.";";
    if (!mysql_query($sql,$dbl))
    {
     fecho("Update player permissions - query ", $sql, " failed : ", mysql_error());
    }
    $tplayers[$changed_tplayer_id]['cw2_player_flags'] = $cw2_player_flags;
   }
  }
 }
  
 $jsarr_tplayers = '
function C_TPlayer (tplayer_sid, tplayer_name, tplayer_permissions)
{
 this.tplayer_sid = tplayer_sid;
 this.tplayer_name = tplayer_name;
 this. tplayer_permissions = tplayer_permissions;
}
TPlayers = new Array();';
 echo $GLOBALS['lang_cw2_tournament']." ".$tournament['name']."<br>";
 echo '
<FORM ID="form_id" action="cw2_edit_permissions.php" method="post">
<table><tr><td>
<SELECT id="TPlayer_sel" onchange="onTPlayerChange();">';
 $count = 0;
 foreach ($tplayers as $tplayer_id => &$tplayer)
 {
  if (($logged_permissions & CW2_TOURNAMENT_MODERATOR) || (($logged_permissions & CW2_CLAN_ADMIN) && ($tplayer['tgroup_id'] == $logged_tgroup_id)))
  {
   echo '
<option value="'.$count.'">'.$tplayer['name']." - ".$tgroups[$tplayer['tgroup_id']]['name'].'</option>';
   $jsarr_tplayers.= '
 TPlayers['.$count.'] = new C_TPlayer('.$tplayer_id.', "'.jst($tplayer['name']).'", '.($tplayer['cw2_player_flags'] & CW2_MAX_CLAN_PERMISSIONS).');';
   $count++;
  }
 }
 echo '
</select>
<INPUT TYPE="HIDDEN" name="tournament_id" value="'.$tournament_id.'">
<INPUT TYPE="HIDDEN" name="player_id" value="'.$logged_player_id.'">
</td>
<td>
';
foreach ($lang_cw2_permissions as $permission_id=>$permission_name)
{
 if ($permission_id & CW2_MAX_CLAN_PERMISSIONS)
 {
  echo '
'.$permission_name.'<INPUT TYPE=CHECKBOX id="permission_'.$permission_id.'" name="permission_'.$permission_id.'"><br/>';
 }
}
echo '
<INPUT TYPE=SUBMIT VALUE="'.$GLOBALS['lang_cw2_save__caps'].'" OnClick="onSubmit();">
</td></tr>
</FORM>

<script type="text/javascript" src="cw2.js"></script>
<script type="text/javascript">
'.$jsarr_tplayers.'
var CW2_MAX_CLAN_PERMISSIONS = '.CW2_MAX_CLAN_PERMISSIONS.';

function onTPlayerChange()
{
 tplayer_sel = document.getElementById("TPlayer_sel");
 idx = tplayer_sel.selectedIndex;
 tplayer_permissions = 0;
 if (idx >= 0)
 {
  tplayer_permissions = TPlayers[idx].tplayer_permissions;
 }
 curr_permission = 1;
 while (curr_permission <= CW2_MAX_CLAN_PERMISSIONS)
 {
  curr_checkbox = document.getElementById("permission_"+curr_permission);
  if (curr_checkbox)
  {
   curr_checkbox.checked = (tplayer_permissions & curr_permission);
  }
  curr_permission *= 2;
 }
 
}

function doOnLoad()
{
 onTPlayerChange();
}

function onSubmit()
{
 tplayer_sel = document.getElementById("TPlayer_sel");
 idx = tplayer_sel.selectedIndex;
 if (idx >= 0)
 {
  form = document.getElementById("form_id");
  addInput(form, "changed_tplayer_id", TPlayers[idx].tplayer_sid);
 }
}
</script>
';
  
 } while (false);
 
?>
</body>
</html>
